// 封装wx.login()，为Promise对象
const login = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout:10000,
      success: (result) => {
        resolve(result)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {}
    })
  })
}

// 封装wx.getSetting()，为Promise对象
const getSetting = () => {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success: (result) => {
        resolve(result)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {}
    });
      
  })
}

// 封装wx.showModal()，为Promise对象
const showModal = (params) => {
  return new Promise((resolve, reject) => {
    wx.showModal({
      title: params.title,
      content: params.content,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          resolve(result)
        } else if (result.cancel) {
          resolve(result)
        }
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {}
    })
  })
}

// 封装wx.showToast，为Promise对象
const showToast = (params) => {
  return new Promise((resolve, reject) => {
    wx.showToast({
      title: params.title,
      icon: params.icon,
      image: '',
      duration: 1500,
      mask: true,
      success: (result) => {
        resolve(result)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {}
    })
  })
}

module.exports = {
  login,
  getSetting,
  showModal,
  showToast
}